package com.agriculture.competition.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.agriculture.common.core.utils.DateUtils;
import com.agriculture.common.security.service.TokenService;
import com.agriculture.competition.domain.JaCompetitionSolution;
import com.agriculture.competition.domain.JaModelClassify;
import com.agriculture.competition.domain.JaModelManage;
import com.agriculture.competition.domain.vo.ClassfiyModelVo;
import com.agriculture.competition.domain.vo.CompetitionGroupVo;
import com.agriculture.competition.domain.vo.JaModelManageVo;
import com.agriculture.competition.mapper.JaClassifyMapper;
import com.agriculture.competition.mapper.JaCompetitionGroupMapper;
import com.agriculture.competition.mapper.JaModelClassifyMapper;
import com.agriculture.competition.mapper.JaModelManageMapper;
import com.agriculture.competition.service.IJaModelManageService;
import com.agriculture.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模型管理Service业务层处理
 *
 * @author agriculture
 * @date 2024-11-13
 */
@Service
public class JaModelManageServiceImpl implements IJaModelManageService {
    @Autowired
    private JaModelManageMapper jaModelManageMapper;

    @Autowired
    private JaClassifyMapper jaClassifyMapper;

    @Autowired
    private JaModelClassifyMapper jaModelClassifyMapper;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private JaCompetitionGroupMapper jaCompetitionGroupMapper;

    /**
     * 查询模型管理
     *
     * @param id 模型管理主键
     * @return 模型管理
     */
    @Override
    public JaModelManage selectJaModelManageById(Long id) {
        JaModelManage jaModelManage = jaModelManageMapper.selectJaModelManageById(id);
        dealModelInfo(jaModelManage);
        //修改访问次数
        JaModelManage jaModelManage1 = new JaModelManage();
        jaModelManage1.setId(jaModelManage.getId());
        Long collectNum = jaModelManage.getCollectNum();
        jaModelManage1.setCollectNum(collectNum + 1L);
        jaModelManageMapper.updateJaModelManage(jaModelManage1);
        return jaModelManage;
    }

    private void dealModelInfo(JaModelManage jaModelManage) {
        if (jaModelManage != null) {
            Map map = jaClassifyMapper.queryUserById(jaModelManage.getCreateUser());
            if (map != null) {
                String userName = (String) map.get("userName");
                jaModelManage.setCreateUserStr(userName);
            }
            List<ClassfiyModelVo> classfiyModelVos = jaModelClassifyMapper.queryClassfiyName(jaModelManage.getId());
            if (!CollectionUtils.isEmpty(classfiyModelVos)) {
                List<Long> collect = classfiyModelVos.stream().map(e -> e.getClassifyId()).collect(Collectors.toList());
                String result = classfiyModelVos.stream().map(s -> s.getName()).collect(Collectors.joining(","));
                jaModelManage.setModelType(result);
                jaModelManage.setClassfiyModelVos(classfiyModelVos);
                jaModelManage.setClassifyIds(collect);
            }
        }
    }

    /**
     * 查询模型管理列表
     *
     * @param jaModelManage 模型管理
     * @return 模型管理
     */
    @Override
    public List<JaModelManage> selectJaModelManageList(JaModelManageVo jaModelManage) {
        List<JaModelManage> jaModelManages = jaModelManageMapper.selectJaModelManageList(jaModelManage);
        if (!CollectionUtils.isEmpty(jaModelManages)) {
            jaModelManages.forEach(s -> {
                dealModelInfo(s);
            });
        }
        return jaModelManages;
    }

    /**
     * 新增模型管理
     *
     * @param jaModelManage 模型管理
     * @return 结果
     */
    @Override
    public int insertJaModelManage(JaModelManage jaModelManage) {

        LoginUser loginUser = tokenService.getLoginUser();
        List<CompetitionGroupVo> jaCompetitionGroups = jaCompetitionGroupMapper.queryLoginUserGroup(loginUser.getUserid());
        if (CollectionUtil.isEmpty(jaCompetitionGroups)) {
            throw new RuntimeException("您没有参赛，请先参加比赛！");
        }
        jaModelManage.setCreateTime(DateUtils.getNowDate());
        jaModelManage.setCreateUser(loginUser.getUserid());
        List<Long> classifyIds = jaModelManage.getClassifyIds();
        String vstrIds = "";
        if (!CollectionUtils.isEmpty(classifyIds)) {
            vstrIds = classifyIds.stream().map(String::valueOf).collect(Collectors.joining(","));
        }
        jaModelManage.setModelType(vstrIds);
        jaModelManage.setCreateUserName(loginUser.getUsername());
        int i = jaModelManageMapper.insertJaModelManage(jaModelManage);
        if (!CollectionUtils.isEmpty(classifyIds)) {
            for (Long id : classifyIds) {
                JaModelClassify jaModelClassify = new JaModelClassify();
                jaModelClassify.setClassifyId(id);
                jaModelClassify.setModelId(jaModelManage.getId());
                jaModelClassifyMapper.insertJaModelClassify(jaModelClassify);
            }
        }
        return i;
    }

    /**
     * 修改模型管理
     *
     * @param jaModelManage 模型管理
     * @return 结果
     */
    @Override
    public int updateJaModelManage(JaModelManage jaModelManage) {
        LoginUser loginUser = tokenService.getLoginUser();
        jaModelManage.setUpdateTime(DateUtils.getNowDate());
        jaModelClassifyMapper.deleteJaModelClassifyByModelId(jaModelManage.getId());
        List<Long> classifyIds = jaModelManage.getClassifyIds();
        if (!CollectionUtils.isEmpty(classifyIds)) {
            String vstrIds = classifyIds.stream().map(String::valueOf).collect(Collectors.joining(","));
            jaModelManage.setModelType(vstrIds);
            for (Long id : classifyIds) {
                JaModelClassify jaModelClassify = new JaModelClassify();
                jaModelClassify.setClassifyId(id);
                jaModelClassify.setModelId(jaModelManage.getId());
                jaModelClassifyMapper.insertJaModelClassify(jaModelClassify);
            }
        }
        jaModelManage.setUpdateUser(loginUser.getUserid());
        return jaModelManageMapper.updateJaModelManage(jaModelManage);
    }

    /**
     * 批量删除模型管理
     *
     * @param ids 需要删除的模型管理主键
     * @return 结果
     */
    @Override
    public int deleteJaModelManageByIds(Long[] ids) {
        return jaModelManageMapper.deleteJaModelManageByIds(ids);
    }

    /**
     * 删除模型管理信息
     *
     * @param id 模型管理主键
     * @return 结果
     */
    @Override
    public int deleteJaModelManageById(Long id) {
        return jaModelManageMapper.deleteJaModelManageById(id);
    }

    @Override
    public List<JaModelManage> queryList(JaModelManageVo jaModelManageVo) {
        LoginUser loginUser = tokenService.getLoginUser();
        List<CompetitionGroupVo> jaCompetitionGroups = jaCompetitionGroupMapper.queryLoginUserGroup(loginUser.getUserid());
        Map<Long, CompetitionGroupVo> collect = new HashMap<>();
        if (CollectionUtil.isNotEmpty(jaCompetitionGroups)) {
            collect = jaCompetitionGroups.stream().collect(Collectors.toMap(CompetitionGroupVo::getId, a -> a, (k1, k2) -> k1));
        }
        List<JaModelManage> jaModelManagesreuslt = new ArrayList<>();
        // jaModelManageVo.setCreateUser(loginUser.getUserid());
        List<JaModelManage> jaModelManages = jaModelManageMapper.selectJaModelManageList(jaModelManageVo);
        if (!CollectionUtils.isEmpty(jaModelManages)) {

            for (JaModelManage vo : jaModelManages) {
                if (collect.containsKey(vo.getGroupId())) {
                    CompetitionGroupVo competitionGroupVo = collect.get(vo.getGroupId());
                    vo.setCompetitionName(competitionGroupVo.getCompetitionName());
                    vo.setGroupName(competitionGroupVo.getGroupName());
                    vo.setSolutionId(competitionGroupVo.getSolutionId());
                    dealModelInfo(vo);
                    jaModelManagesreuslt.add(vo);
                }
            }
        }
        return jaModelManagesreuslt;
    }

    @Override
    public List<JaModelManage> queryModelByClassfiyId(Long id) {
        List<JaModelManage> jaModelManages = jaModelManageMapper.queryModelByClassfiyId(id);
        return jaModelManages;
    }

    @Override
    public List<JaModelManage> queryModelByCompetitionUser(JaCompetitionSolution jaCompetitionSolution) {
        //LoginUser loginUser = tokenService.getLoginUser();
        // jaCompetitionSolution.setGroupId(loginUser.getUserid());
        List<JaModelManage> jaModelManages = jaModelManageMapper.queryModelInfo(jaCompetitionSolution);
        return jaModelManages;
    }
}
